/*********************************************************************
 * Módulo 3. Tutorial de desarrollo con NME. Bucle de juego
 * Programación Multimedia y Juegos
 * Autor: David Vallejo Fernández  david.vallejo@tegnix.com
 * Autor: Carlos González Morcillo  carlos.gonzalez@tegnix.com
 *
 * You can redistribute and/or modify this file under the terms of the
 * GNU General Public License ad published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * and later version. See <http://www.gnu.org/licenses/>.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details. 
 *********************************************************************/

package;

import com.eclecticdesignstudio.motion.Actuate;
import flash.events.Event;
import nme.ui.Keyboard;
import nme.events.KeyboardEvent;
import nme.events.MouseEvent;
import flash.Lib;
import nme.display.Bitmap;
import nme.Assets;
import nme.display.StageAlign;
import nme.display.StageScaleMode;

class PlayState extends GameState {

	public static var _instance:PlayState;
	private var _circles:Array<Circle>;
	private var _logo:Bitmap;
	public var bolas : Int;

	private function new () {
		super(); 
		_circles = new Array<Circle>();
		_logo = new Bitmap (Assets.getBitmapData ("assets/background.png"));
	}

	// Patrón Singleton.
	public static function getInstance():PlayState {
		if (PlayState._instance == null)
			PlayState._instance = new PlayState();
		return PlayState._instance;
	}

	override function enter() : Void { 
		Lib.current.stage.align = StageAlign.TOP_LEFT;
		Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
		// Al entrar añade el logo para que lo renderice.
		addChild(_logo);
	}

	override function exit() : Void { 
		removeChild(_logo);
	}

	// Pause la animación de los círculos creados.
	override function pause() : Void {
		removeChild(_logo);
		Actuate.pause(this);
		for (circle in _circles) {
			circle.pause();
		}
	}

	// Reanuda la animación de los círculos creados.
	override function resume() : Void {
		addChild(_logo);
		Actuate.resume(this);
		for (circle in _circles) {
			circle.resume();
		}
	}

	override function keyPressed (event:KeyboardEvent) : Void { 
		// Transición al estado de pausa.
		if (event.keyCode == Keyboard.SPACE) {
			pushState(PauseState.getInstance());
		}
	}

	override function keyReleased (event:KeyboardEvent) : Void { }

	// Crea un nuevo círculo u oscurece uno ya existente.
	override function mouseClicked (event:MouseEvent) : Void {
		var x:Float = event.stageX;
		var y:Float = event.stageY;

		// ¿Colisión sobre algún círculo?
		for (circle in PlayState.getInstance().getCircles()) {
			if (circle.impact(x, y)) {
				// Si hay colisión, oscurece el color del círculo.
				circle.fillInBlack();
				bolas--;
				return;
			}
		}

		createCircle ();
	} 

	override function frameStarted (event:Event) : Void { }
	override function frameEnded (event:Event) : Void { }

	public function getCircles ():Array<Circle> {
		return _circles;
	}
 
	// Crea y un círculo y lo anima.
	private function createCircle ():Void {

		if (bolas < 10) {
			var circle:Circle = new Circle ();
			_circles.push(circle);
			addChild(circle);
			circle.animate();
			bolas++;
		} else {
			bolas = 0;
			for (circle in PlayState.getInstance().getCircles()) {
				removeChild(circle);
			}
			_circles = new Array<Circle>();
			pushState(OverState.getInstance());
		}
	}

	public function setBolas(num:Int):Void {
		bolas = num;
	}
}
